package com.collectplus.express.self;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.collectplus.express.BaseActivityWithScroll;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.BagBean;

import java.util.ArrayList;
import java.util.List;

import droid.frame.activity.HandlerMgr;
import droid.frame.app.Callback;
import droid.frame.utils.Android;
import droid.frame.view.CodeLinearLayout;
import droid.frame.view.ViewPagerExt;


/**
 * 手动输入退货袋
 *
 * @author coffee <br>
 *         2016-10-20上午11:09:10
 */
public class SelfBagInputActivity extends BaseActivityWithScroll implements OnClickListener {

    private final int PAGE_SIZE = 2;// ViewPager数量
    private Button mSubmit;

    private CodeLinearLayout mInputLayout;
    private final int CODE_LENGTH = 10;

    private ViewPagerExt mViewPager;
    private LinearLayout mGagLayout;
    private TextView mTitleTV;//头部随着图片滑动显示的文字
    private TextView mBagCodeTextTV;//编号对应的描述

    /**
     * 从首页过来的返回数据在当前处理
     */
    private int from = 0;

    @Override
    @SuppressLint("ResourceAsColor")
    protected void onCreate(Bundle savedInstanceState) {
        setScrollHeight(100);
        super.onCreate(savedInstanceState);
        from = getIntent().getIntExtra("from", 0);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.self_bag_input);
        super.findViewById();
        getAppTitle().setCommonTitle("绑定投递袋");
        // mInputCode = (EditText) findViewById(R.id.self_input_code);

        mTitleTV = (TextView) findViewById(R.id.bag_tv);
        mBagCodeTextTV = (TextView) findViewById(R.id.bag_code_tv);

        mTitleTV.setText("火箭投递袋");
        mBagCodeTextTV.setText("请输入投递袋上的编码");

        mInputLayout = (CodeLinearLayout) findViewById(R.id.self_input_layout);
        mInputLayout.setCallback(new Callback() {
            @Override
            public void execute(Object... result) {
                String code = mInputLayout.getText();
                if (code.length() == CODE_LENGTH) {
                    onClick(mSubmit);
                    setButtonEnableStyle(mSubmit, true);
                }
            }
        });
        // 输入格式化
        mSubmit = (Button) findViewById(R.id.self_input_submit);
        setButtonEnableStyle(mSubmit, false);
        mSubmit.setOnClickListener(this);
        //投递袋和投递面单的切换
        mViewPager = (ViewPagerExt) findViewById(R.id.view_pager);
        mGagLayout = (LinearLayout) findViewById(R.id.bag_dot_layout);
        loadImage();
        selectPage(0);
        mViewPager.addOnPageChangeListener(mViewPager.new ViewPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                selectPage(position);
                //最后一页
                if (position == mGagLayout.getChildCount() - 1) {
                    mTitleTV.setText("火箭投递面单");
                    mBagCodeTextTV.setText("请输入投递面单上的编码");
                } else {
                    mTitleTV.setText("火箭投递袋");
                    mBagCodeTextTV.setText("请输入投递袋上的编码");
                }

            }
        });
    }

    private void loadImage() {
        List<View> pager = new ArrayList<View>();
        for (int i = 0; i < PAGE_SIZE; i++) {
            int resId = getResources().getIdentifier("send_bag_pic_" + (i + 1), "drawable", getPackageName());
            LinearLayout parms = new LinearLayout(getContext());
            parms.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            parms.setBackgroundResource(resId);
            pager.add(parms);
        }
        mViewPager.setViewItems(pager, false);
    }

    /**
     * 设置选中的页
     *
     * @param position
     */
    private void selectPage(int position) {
        for (int i = 0; i < mGagLayout.getChildCount(); i++) {
            ImageView child = (ImageView) mGagLayout.getChildAt(i);
            if (position == i) {
                child.setBackgroundResource(R.drawable.self_dot_light);
            } else {
                child.setBackgroundResource(R.drawable.self_dot_dark);
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DEL) {
            updateButtonEnableState();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    /**
     * {@link SelfMainActivity#handleMessage(Message)}
     */
    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            /**
             * 请求结果的逻辑在SelfMainActivity处理<br>
             * {@link SelfMainActivity#handleMessage(Message)}
             */
            case MsgID.self_bagcode_check:
                cancelLoadingDialog();
                if (from == 1) {// 从首页进来在这处理
                    AppResult<BagBean> result = JSON.parser(msg.obj, BagBean.class);
                    if (result.isSuccess() && result.getResult() != null) {
                        HandlerMgr.sendMessage(MsgID.send_bag_info, result.getResult());
                        Android.hideSoftInput(getContext());
                        finish();
                    } else {
                        showToast(result.getMessage());
                    }
                    return true;
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onClick(View v) {
        String code = mInputLayout.getText().replace(" ", "");
        if (code.length() < 10) {
            // showToast("请输入投递袋编号");
        } else {
            switch (v.getId()) {
                case R.id.self_input_submit:
                    if (isEmpty(code)) {
                        showToast("请输入投递袋编号");
                        return;
                    }
                    showLoadingDialog("数据校验");
                    AppHttp.getInstance().selfBagCodeCheck(code);
                    break;
            }
        }
    }

    private void updateButtonEnableState() {
        String code = mInputLayout.getText();
        if (code.length() == CODE_LENGTH) {
            setButtonEnableStyle(mSubmit, true);
        } else {
            setButtonEnableStyle(mSubmit, false);
        }
    }

    @Override
    public void finish() {
        super.finish();
        Android.hideSoftInput(getContext());
    }
}
